package com.tianji.learning.mq;


import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class LearningPointsListener {
    private final IPointsRecordService pointsRecordService;


    /**
     * 签到
     * */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sign.points.queue",durable = "true"),
            exchange =@Exchange(name=MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =MqConstants.Key.SIGN_IN
    ))
    public void listenSignInMessage(SignInMessage message){
        log.info("收到签到积分消息：{}",message);
        if (message == null){
            return;
        }
        pointsRecordService.saveRecord(message, PointsRecordType.SIGN);
        log.info("发送签到积分成功：{}", message);
    }

    /**
     * 学习视频
     * */
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name = "session.points.queue",durable = "true"),
            exchange =@Exchange(name=MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =MqConstants.Key.LEARN_SECTION
    ))
    public void listenLearnSectionMessage(SignInMessage message){
        log.debug("收到学习视频积分消息：{}",message);
        if (message == null){
            return;
        }
        pointsRecordService.saveRecord(message, PointsRecordType.LEARNING);
        log.info("发送学习视频积分成功：{}", message);
    }
    /**
     * 问答与评论
    * */
    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name = "qa.points.queue",durable = "true"),
            exchange =@Exchange(name=MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key =MqConstants.Key.WRITE_REPLY
    ))
    public void listenWriteReplyMessage(SignInMessage message){
        log.info("收到问答与评论积分消息：{}",message);
        if (message == null){
            return;
        }
        if (message.getPoints()==5) {
            pointsRecordService.saveRecord(message, PointsRecordType.QA);
        }else {
            pointsRecordService.saveRecord(message, PointsRecordType.COMMENT);
        }
        log.info("发送问答评论积分成功：{}", message);
    }
}
